//
//  UIButton+HBExtension.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/27.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit

extension UIButton {
    convenience init(title: String, image: String, textColor: UIColor) {
        self.init()

        self.setTitle(title, forState: .Normal)
        self.setImage(UIImage(named: image), forState: .Normal)
        self.setTitleColor(textColor, forState: .Normal)
    }

    // 这次遇到bug了, 变不过去
    func buttonWithLeftTitle(fontSize: CGFloat) {

        let spacing: CGFloat = 0.0;     // the space between the image and text
        let imageSize: CGSize = self.imageView!.frame.size;
        let titleSize: CGSize = self.titleLabel!.frame.size;

        self.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -titleSize.width * 2 - spacing)
        self.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width * 2 - spacing, 0.0, 0.0)
    }

}

func buttonWithUpsideImageAndDownsideTitle(button: UIButton, fontSize: CGFloat) {
    let spacing: CGFloat = 0.0;     // the space between the image and text
    let imageSize: CGSize = button.imageView!.frame.size;
    let titleSize: CGSize = button.titleLabel!.frame.size;
//    var textSize: CGSize = sizeWithText((button.titleLabel?.text)!, textFont: UIFont.systemFontOfSize(fontSize), maxSize: CGSizeMake(button.bounds.width, button.bounds.height))
//    let frameSize: CGSize = CGSizeMake(textSize.width, textSize.height)
//    if textSize.width + 0.5 < frameSize.width {
//        textSize.width = frameSize.width
//    }

    let totalHeight = imageSize.height + titleSize.height + spacing
    button.imageEdgeInsets = UIEdgeInsetsMake(-(totalHeight - imageSize.height), 0.0, 0.0, -titleSize.width)
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(totalHeight - titleSize.height), 0.0)
}

func buttonWithLeftTextAndRightImage(button: UIButton, fontSize: CGFloat) {
    let spacing: CGFloat = 0.0;     // the space between the image and text
    let imageSize: CGSize = button.imageView!.frame.size;
    let titleSize: CGSize = button.titleLabel!.frame.size;

    button.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -titleSize.width * 2 - spacing)
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width * 2 - spacing, 0.0, 0.0)
}

///*----------------------------- btn inset -----------------------------*/
///* a negative value expands the edge, while a positive value shrinks it to move it closer to the center. */
//
//// the space between the image and text
//let spacing: CGFloat = 0.0;
//
//let imageSize: CGSize = btn.imageView!.frame.size;
//let titleSize: CGSize = btn.titleLabel!.frame.size;
//let totalHeight = imageSize.height + titleSize.height + spacing
//
//// up the image
//btn.imageEdgeInsets = UIEdgeInsetsMake(-(totalHeight - imageSize.height), 0.0, 0.0, -titleSize.width);
//
//// down the text
//btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(totalHeight - titleSize.height), 0.0);
//
//btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
///*----------------------------- btn inset -----------------------------*/